﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.Shell.Hosting.OpenXmlFileProjectStoreInternals
{
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Xml.Linq;

    internal class ProjectStoreFolderCollection : IProjectStoreFolderCollection
    {
        private readonly static XNamespace NS = OpenXmlFileProjectStore.NS;

        private readonly OpenXmlFileProjectStore mStore;
        private readonly XElement mFoldersElement;
        private readonly List<IProjectStoreFolder> mFolders = new List<IProjectStoreFolder>();

        #region .ctor

        internal ProjectStoreFolderCollection(OpenXmlFileProjectStore store, XElement foldersElement)
        {
            Debug.Assert(foldersElement.Name == NS + "Folders");

            // Load child folders
            mFoldersElement = foldersElement;

            foreach (var folderElement in foldersElement.Elements(NS + "Folder"))
            {
                mFolders.Add(new ProjectStoreFolder(store, folderElement));
            }

            // Need the store for folder creation
            mStore = store;
        }

        #endregion

        #region IProjectStoreFolderCollection Members

        int IProjectStoreFolderCollection.Count
        {
            get { return mFolders.Count; }
        }

        IProjectStoreFolder IProjectStoreFolderCollection.this[int index]
        {
            get { return mFolders[index]; }
        }

        #endregion

        #region IEnumerable<IProjectStoreFolder> Members

        IEnumerator<IProjectStoreFolder> IEnumerable<IProjectStoreFolder>.GetEnumerator()
        {
            return mFolders.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return mFolders.GetEnumerator();
        }

        #endregion
    }
}
